using System;
using System.Collections.Generic;
using System.Net;
using System.Net.NetworkInformation;
using Microsoft.Win32;
using System.IO;

public class GetHostInformation
    {
        //Static Location of the Network Adapters currently in Use of the client
        private string adapterLocation = @"SYSTEM\CurrentControlSet\Control\Network\{4D36E972-E325-11CE-BFC1-08002BE10318}\";
        //Fields for the Local System Drive, Formatted Hostname and the Original Hostname
        private string drive = string.Empty;
        private string fname = string.Empty;
        private string name = string.Empty;
        int l;
        /// <summary>
        /// Gathers Host Information
        /// </summary>
        /// <returns>MachineInfo</returns>
        public MachineInfo getinfo()
        {
            drive = Environment.GetFolderPath(Environment.SpecialFolder.System).Substring(0, 2);
            try
            {
                // create an instance of MachineInfo
                // hold machine and adapter info
                MachineInfo mInfo = new MachineInfo();
                List<AdapterInformation> adapterInfo = new List<AdapterInformation>();
                mInfo.AdapterInformation = adapterInfo;
                // get host name
                string host = Dns.GetHostName();
                try
                {
                    name = System.Security.Principal.WindowsIdentity.GetCurrent().Name.ToString();
                    l = name.Length;
                    fname = name.Substring((l - 4), l);
                    //mInfo.ComputerID = int.Parse(fname);
                    mInfo.ComputerID = 9999;
                }
                catch (Exception)
                {
                    mInfo.ComputerID = 9999;
                }
                mInfo.HostName = host;
                // registry key in Local Machine
                RegistryKey key = Registry.LocalMachine; 

                // obtain a reference to all network interfaces in the machine
                NetworkInterface[] nAdapters = NetworkInterface.GetAllNetworkInterfaces();
                foreach (NetworkInterface adapter in nAdapters)
                {
                    string adapterKey = adapterLocation + adapter.Id + @"\Connection";
                    // create an instance of AdapaterInformation
                    AdapterInformation ai = new AdapterInformation();
                    // list of ip information type string
                    List<String> ipInfo = new List<string>();
                 
                    key.OpenSubKey(adapterKey, false);

                    ai.PhysicalAddress = adapter.GetPhysicalAddress().ToString();
                    ai.AdapterName = adapter.Description;
                    // if theres a key
                    if (key != null)
                    {
                        // get network configuration
                        IPInterfaceProperties ip = adapter.GetIPProperties();
                        UnicastIPAddressInformationCollection ipcol = ip.UnicastAddresses;
                        // build list of uips in text file
                        foreach (UnicastIPAddressInformation uip in ipcol)
                        {

                            String address = uip.Address.ToString();
                            ipInfo.Add(address);
                            StreamWriter sw = new StreamWriter(drive+@"\ReserveMeLogs\IPDebug.txt");
                            sw.WriteLine(address);
                            sw.Close();
                        }
                    }
                    ai.AdapterIP = ipInfo;
                    // add them to adapterinfo
                    adapterInfo.Add(ai);
                    ai = null;
                }
                // finish with that file
                key.Close();
                mInfo.AdapterInformation = adapterInfo;

                return mInfo;
            }
           // if above throws exception write error to 'GHIError.txt'
            catch (Exception ex)
            {
                StreamWriter sw = new StreamWriter(drive+@"\ReserveMeLogs\GHIError.txt");
                sw.WriteLine("{0} \n {1} \n {2} \n {3}",ex.Message, ex.Data, ex.StackTrace, ex.InnerException);
                sw.WriteLine(fname);
                sw.WriteLine(name);
                sw.WriteLine(l);
                sw.Close();
                return null;
            }
        }
    }